package lambdasinaction.chap11;

import java.util.Random;

import static lambdasinaction.chap11.Util.delay;

/**
 * Created by ThinkPad on 2017/8/28.
 */
public class Shop {
    private final String name;
    private final Random random;

    public Shop(String name) {
        this.name = name;
        random = new Random(name.charAt(0) * name.charAt(1) * name.charAt(2));
    }

    /**
     * 该方法内部实现会查询商店的数据库,也有可能执行一些其他的耗时的任务
     * 本章内容中采用delay方法模拟这些长期运行的方法的执行,它会人为的引
     * 入1秒的延迟.
     * @See Util#delay
     * @param product
     * @return
     */
    public String getPrice(String product) {
        double price = calculatePrive(product);
        Discount.Code code = Discount.Code.values()[random.nextInt(Discount.Code.values().length)];
        return name + ":" + price + ":" + code;
    }

    private double calculatePrive(String product) {
        delay();
        return random.nextDouble() * product.charAt(0) + product.charAt(1);
    }

    public String getName() {
        return name;
    }
}
